use yew::prelude::*;

// 定义消息类型
pub enum Msg {
    UpdateName,
    // UpdateAge,
}

// 定义用户结构体
pub struct User {
    name: String,
}

#[derive(Properties, PartialEq)]
pub struct UserProps {
    #[prop_or(28)]
    pub age: i32,
    // pub on_update_age: Callback<()>,
    pub children: Html,
}

impl Component for User {
    type Message = Msg;
    type Properties = UserProps;

    fn create(_ctx: &Context<Self>) -> Self {
        Self {
            name: "hboot".to_string(),
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::UpdateName => {
                self.name = "admin".to_string();
                true
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        // <button onclick={ctx.link().callback(|_| Msg::UpdateName )}>{"更新"}</button>
        html! {
            <div class="user-center">
                <button onclick={ctx.link().callback(|_| Msg::UpdateName )}>{"更新name"}</button>

                <h3>{format!("姓名：{}",self.name.to_string())}</h3>

                {ctx.props().children.clone()}
            </div>
        }
    }
}
